<template>
  <el-row style="margin-bottom: 20px;padding: 0 20px;margin-left: -70px" id="head">
    <el-col :span="8" class="status-col">
      <el-button class="status-label" @click="doListAll">待接单</el-button>
    </el-col>
    <el-col :span="8" class="status-col">
      <el-button class="status-label" @click="doPeiSong">配送中</el-button>
    </el-col>
    <el-col :span="8" class="status-col">
      <el-button class="status-label" @click="doSuccess">已完成</el-button>
    </el-col>
  </el-row>
  <div class="card">
    <!--表格-->
    <el-row justify="start" >
      <el-table
          :data="dingdanList"
          style="width: 100%;"
      >
        <div style="display: none"><el-table-column prop="psid" label="配送id"></el-table-column></div>
        <div style="display: none"><el-table-column prop="pszt" label="配送状态"></el-table-column></div>
        <el-table-column prop="ddid" label="订单id"></el-table-column>
        <el-table-column prop="name" label="商家名称"></el-table-column>
        <el-table-column prop="yhnc" label="用户昵称"></el-table-column>
        <el-table-column prop="phone" label="手机号"></el-table-column>
        <el-table-column prop="dddz" label="地址"></el-table-column>
        <el-table-column label="操作" >
          <template #default="scope" >
            <div v-if="scope.row.pszt === 3" style="display: flex;padding:1px 0">
              <el-button type="primary" @click="accept(scope.row)">接单</el-button>
            </div>
            <div v-if="scope.row.pszt === 6" style="display: flex;padding:1px 0">
              <el-button type="success" @click="delivered(scope.row)">已送达</el-button>
            </div>
            <div v-if="scope.row.pszt === 9" style="display: flex;padding:1px 0">
              <el-button type="success" disabled>已完成</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-row>
  </div>


  <!--  详情页面  -->
  <el-dialog v-model="detailDialogVisible" title="订单信息" width="60%" top="10vh" style="height: 75%" destroy-on-close>
    <show-d-d-x-q
        ref="detailPageRef"
        @flushPage="reset"
        @closeDialog="detailDialogVisible = false">
    </show-d-d-x-q>
  </el-dialog>
  <!-- 新增 dialog-->
  <el-dialog v-model="addDialogVisible" title="新增订单" width="40%" top="2vh" destroy-on-close>
    <add-dingdan
        @flushPage="reset"
        @closeDialog="addDialogVisible = false">
    </add-dingdan>
  </el-dialog>


</template>

<script setup >
import { onMounted, ref} from 'vue'
import axios from "axios";
import {ElMessage} from "element-plus";

const userInfo = ref(JSON.parse(localStorage.getItem('userInfo')))
const dingdanList = ref();  // 订单信息列表

onMounted(() => {
  doListAll();
});

//查询所有数据
const doListAll = async () => {
  const {data} = await axios.get('/api/peisong/all')
  console.log(data)
  if(data.code === 200){
    dingdanList.value = data.data;
  }
}


const addDialogVisible = ref(false)
const showAddDialog = () => {
  addDialogVisible.value = true;
}
const peisong = ref({
  psid:'',
  psks:'',
  pszt:'',
  qsid:'',
})
const accept = async (row) => {
  peisong.value.psid = row.psid;
  const shanghaiDate = new Date(Date.now() + 8 * 60 * 60 * 1000);
  peisong.value.psks = shanghaiDate.toISOString().slice(0, 19).replace('T', ' ');
  peisong.value.pszt = 6;
  peisong.value.qsid = userInfo.value.qsid;
  const {data} = await axios.put('/api/peisong',peisong.value)
  console.log(data)
  if (data.code === 200){
    ElMessage.success('接单成功');
    await doListAll()
  }
}


const doPeiSong = async () => {
  dingdanList.value = [];
  const {data} = await axios.get(`/api/peisong/psing/${userInfo.value.qsid}`)
  if(data.code === 200){
    dingdanList.value = data.data;
  }
}

const delivered = async (row) => {
  peisong.value.psid = row.psid;
  peisong.value.pszt = 9;
  const shanghaiDate = new Date(Date.now() + 8 * 60 * 60 * 1000);
  peisong.value.psjs = shanghaiDate.toISOString().slice(0, 19).replace('T', ' ');
  const {data} = await axios.put('/api/peisong',peisong.value)
  console.log(data)
  if (data.code === 200){
    ElMessage.success('已送达');
    await doPeiSong()
  }
}

const doSuccess = async () => {
  dingdanList.value = [];
  const {data} = await axios.get(`/api/peisong/success/${userInfo.value.qsid}`)
  if(data.code === 200){
    dingdanList.value = data.data;
  }
}

</script>

<style scoped>
.card{
  margin-left: -50px;
  margin-right: -30px;
  background-color: white;
  padding: 10px;
  border-radius: 5px;
  box-shadow: 0 0 8px rgba(0,0,0, .12);
}
#head{
  display: flex;
  width: 60%;
  justify-content: center;
  align-items: center;
  height: 50px;
  text-align: center;
}
.status-col {
  font-family: "Inter", "Helvetica Neue", Arial, "PingFang SC", "Hiragino Sans GB",
  "Microsoft YaHei", "微软雅黑", sans-serif;
  font-size: 15px;
  letter-spacing: 1px;
}
.el-button {
  border: 1px solid #e3e8ee;
  border-radius: 2px;
}
.status-col:first-child{
  border-bottom-left-radius: 5px;
  border-top-left-radius: 5px;
}
.status-col:last-child{
  border-bottom-right-radius: 5px;
  border-top-right-radius: 5px;
}
.status-label {
  display: inline;
  width: 100%;
  height: 100%;
  line-height: 26px;
  text-align: center;
  cursor: pointer;
}
.status-label2 {
  display: inline-block;
  width: 80px;        /* 固定标签宽度，让文字对齐 */
  text-align: right;  /* 文字右对齐 */
  margin-right: 8px;  /* 和输入框/选择器保持间距 */
}

</style>